#include "imagehelper.h"
#include "svgconvertmainwindow.h"
#include "ui_svgconvertmainwindow.h"

#include <QFileDialog>
#include <QMimeData>

SvgConvertMainWindow::SvgConvertMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::SvgConvertMainWindow)
{
    ui->setupUi(this);

    ui->cb_type->addItem("png", 0);
    ui->cb_type->addItem("jpg", 1);
//    ui->cb_type->addItem("ico", 2);

    connect(ui->size_width, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, [this](int){
        do_preview();
    });
    connect(ui->size_height, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, [this](int){
        do_preview();
    });
    connect(ui->cb_type, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &SvgConvertMainWindow::do_preview);


    ui->menuFile->addAction("打开 SVG 文件", [this](){
        QString filePath = QFileDialog::getOpenFileName(nullptr, "选择 SVG 文件", "", "SVG (*.svg)");
        if (!filePath.isEmpty()) {
            QFile file(filePath);
            if (file.open(QIODevice::ReadOnly)) {
                ui->icon_svg->setPlainText(file.readAll());
            }
        }
    });
}

SvgConvertMainWindow::~SvgConvertMainWindow()
{
    delete ui;
}

void SvgConvertMainWindow::on_icon_svg_textChanged()
{
    do_preview();
}

void SvgConvertMainWindow::dragEnterEvent(QDragEnterEvent *event)
{
    const QMimeData *mimeData = event->mimeData();
    if (mimeData->hasUrls() && mimeData->urls().at(0).isLocalFile()) {
        event->accept();
    }
}

void SvgConvertMainWindow::dropEvent(QDropEvent *event)
{
    const QMimeData *mimeData = event->mimeData();
    QString filePath = mimeData->urls().at(0).toLocalFile();
    if (filePath.endsWith(".svg")) {
        QFile file(filePath);
        if (file.open(QIODevice::ReadOnly)) {
            ui->icon_svg->setPlainText(file.readAll());
            file.close();

            ui->statusbar->showMessage("已导入:" + file.fileName());
        }
    }
}

void SvgConvertMainWindow::on_pb_export_clicked()
{
    QSize svg_size(ui->size_width->value(), ui->size_height->value());
    QByteArray svg_data = ui->icon_svg->toPlainText().toUtf8();

    switch (ui->cb_type->currentData().toInt()) {
    case 0:{
        QString path = QFileDialog::getSaveFileName(nullptr, "图片导出位置", "新的svg转png图片.png", "*.png");
        if (path.isEmpty()) {
            return;
        }
        QPixmap pixmap = ImageHelper::pixmapFromSvgString(svg_size, svg_data);
        pixmap.save(path, "PNG");
    } break;
    case 1: {
        QString path = QFileDialog::getSaveFileName(nullptr, "图片导出位置", "新的svg转jpg图片.jpg", "*.jpg");
        if (path.isEmpty()) {
            return;
        }
        QPixmap pixmap = ImageHelper::pixmapFromSvgString(svg_size, svg_data);
        pixmap.save(path, "JPG");
    } break;
//    case 2: {
//        QString path = QFileDialog::getSaveFileName(nullptr, "图片导出位置", "新的svg转ico图片.icon", "*.icon");
//        if (path.isEmpty()) {
//            return;
//        }
//        QPixmap pixmap = ImageHelper::pixmapFromSvgString(svg_size, svg_data);
//        pixmap.save(path, "JPG");
//    }  break;
    default:
        break;
    }
}

void SvgConvertMainWindow::do_preview(int index)
{
    QSize svg_size(ui->size_width->value(), ui->size_height->value());
    QByteArray svg_data = ui->icon_svg->toPlainText().toUtf8();

    switch (index) {
    case 0:{
        QPixmap pixmap = ImageHelper::pixmapFromSvgString(svg_size, svg_data);
        if (pixmap.size().width() > ui->icon_png->size().width()
            || pixmap.size().height() > ui->icon_png->size().height()) {
            ui->icon_png->setPixmap(pixmap.scaled(ui->icon_png->size(), Qt::KeepAspectRatio));
        } else {
            ui->icon_png->setPixmap(pixmap);
        }
    } break;
    case 1: {
        QImage image = ImageHelper::imageFromSvgString(svg_size, svg_data);
        ui->icon_png->setPixmap(QPixmap::fromImage(image));
    } break;
    case 2: {
        QIcon icon = ImageHelper::iconFromSvgString(svg_size, svg_data);
        ui->icon_png->setPixmap(icon.pixmap(ui->icon_png->size()));
    }  break;
    default:
        break;
    }
}

